import {IOriginVertexData, IOriginWireFrameData} from "./originDtata";


interface IPair{
    [index:string]:number
}

function containVP(lineIndicesPairs:IPair[], i0:number, i1:number) {
    let item = lineIndicesPairs.find(item => {
        return (item.i0 == i0 && item.i1 == i1) || (item.i0 == i1 && item.i1 == i0);
    });
    return item != null;
}


/**
 * 计算 线框的 indices
 * @param info:{
    positions: number[],
    normals: number[],
    texCoords: number[],
    indices: number[]
    }
 */
export function calWireFrame(info:IOriginVertexData) :IOriginWireFrameData{
    // 获取所有的线段
    // 排除掉重复的
    let lineIndicesPairs:IPair[] = [];
    let indices = info.indices;
    let facelen = indices.length / 3;
    for (let i = 0; i < facelen; ++i) {
        let i0 = indices[3 * i];
        let i1 = indices[3 * i + 1];
        let i2 = indices[3 * i + 2];
        if (!containVP(lineIndicesPairs, i0, i1)) {
            lineIndicesPairs.push({i0: i0, i1: i1});
        }
        if (!containVP(lineIndicesPairs, i1, i2)) {
            lineIndicesPairs.push({i0: i1, i1: i2});
        }
        if (!containVP(lineIndicesPairs, i2, i0)) {
            lineIndicesPairs.push({i0: i2, i1: i0});
        }
    }

    // 计算 wireframe 需要的 indices
    let newIndices:number[] = [];
    lineIndicesPairs.forEach(item => {
        newIndices.push(item.i0, item.i1);
    });
    return {
        positions:info.positions,
        indices:newIndices
    }
}

